<?php
class mdl_category extends ujn_model {
	
	/*
	 * 频道总排行
	 */
	function find_list_all($catid='', $siteid, $limit = 10) { // 指定频道总排行 前10条
		
		if($catid){
			$_where = " `catid` in ({$catid}) and";
		}else{
			$_where = "";
		}
		$sql =<<<EOT
select hitsid from `v9_hits`
		where{$_where} hitsid like 'c-{$siteid}-%'
		order by views desc
		limit {$limit};
EOT;
		$o = array();
		foreach($this->mysql()->find_all($sql) as $d) {
			$d['hitsid'] = explode('-', $d['hitsid']);
			if(isset($d['hitsid'][2]) && (isset($d['hitsid'][1]) && $d['hitsid'][1] == $siteid)) {
				$o[] = $d['hitsid'][2];
			}
		}
		
		if(empty($o)) return array();
		$o = implode('\',\'', $o);
		$sql =<<<EOT
select * from `v9_news`
		where `id` in ('{$o}');
EOT;
		$d = $this->mysql()->find_all($sql);
		return $d;
	}
	
	/*
	 * 频道本月排行
	 */
	function find_list_mon($catid='', $siteid, $limit = 10) { // 
		
		if($catid){
			$_where = " `catid` in ({$catid}) and";
		}else{
			$_where = "";
		}
		$sql =<<<EOT
select hitsid from `v9_hits`
	where{$_where} hitsid like 'c-{$siteid}-%'
	order by monthviews desc
	limit {$limit};
EOT;
		$o = array();
		foreach($this->mysql()->find_all($sql) as $d) {
			$d['hitsid'] = explode('-', $d['hitsid']);
			if(isset($d['hitsid'][2]) && (isset($d['hitsid'][1]) && $d['hitsid'][1] == $siteid)) {
				$o[] = $d['hitsid'][2];
			}
		}
		
		if(empty($o)) return array();
		$o = implode('\',\'', $o);
		$sql =<<<EOT
select * from `v9_news`
	where `id` in ('{$o}');
EOT;
		$d = $this->mysql()->find_all($sql);
		return $d;
	}
	
	/**
	 * 子栏目列表
	 */
	function find_sub($catids, $siteid, $limit = 10) {
		$_where = '1';
		if(!empty($catids)) {
			if(is_array($catids)) {
				$catids = implode('\',\'', $catids);
				$_where = "`catid` in ('{$catids}')";
			}else{
				$_where = "`catid` = '{$catids}'";
			}
		}
		$sql =<<<EOT
select * from `v9_news`
	where {$_where}
	order by posids desc, inputtime desc
	limit {$limit};
EOT;
		$d = $this->mysql()->find_all($sql);
		return $d;
	}
	
	/*
	 * 子栏目图片列表
	 */
	function find_sub_img($catids, $siteid, $limit = 10){
		$_where = '1';
		if(!empty($catids)) {
			if(is_array($catids)) {
				$catids = implode('\',\'', $catids);
				$_where = "`catid` in ('{$catids}') and `thumb` != ''";
			}else{
				$_where = "`catid` = '{$catids}' and `thumb` != ''";
			}
		}
		$sql =<<<EOT
select * from `v9_news`
	where {$_where}
	order by posids desc, inputtime desc
	limit {$limit};
EOT;
		$d = $this->mysql()->find_all($sql);
		return $d;
	}

	/*
	 * 获取最新的下载数据
	 */
	function find_downloads($catid, $siteid, $limit = 10) {
		if(empty($catid)) return array();
		$_where = '';
		if(is_array($catid)) {
			$catid = implode('\',\'', $catid);
			$_where = "where `catid` in ('{$catid}')";
		} else {
			$_where = "where `catid` = '{$catid}'";
		}
		$sql =<<<EOT
select * from `v9_download` {$_where}
	order by inputtime desc
	limit {$limit};
EOT;
		$d = $this->mysql()->find_all($sql);
		return $d;
	}
	
	/*
	 * 获取指定栏目信息
	 */
	function get($catid){
		return $this->mysql()->find_one("select * from `v9_category` where `catid` = '{$catid}';");
	}
	
	/*
	 * 获取栏目权限
	 */
	function get_priv($siteid = 1, $action = 'init') {
		$curr = mdl_account::get_instance()->get_current();
		$sql = "select `catid` from `v9_category_priv` where `siteid` = '{$siteid}' and `roleid`='{$curr['urole']}' and `action` = '{$action}';";
		if($curr['urole'] == '1') {
			return array();
// 			$sql = "select `catid` from `v9_category_priv` where `siteid` = '{$siteid}' and `action` = '{$action}';";
		} 
		
		$cids = array();
		foreach($this->mysql()->find_all($sql) as $d) {
			$cids[$d['catid']] = $d['catid'];
		}
		return $cids;
	}
  
	function get_sub($siteid, $catids, $is_parent = false) {
		$where = array();
		$where[] = "`siteid`='{$siteid}'";
		
		if(!empty($catids)) {
			$cids = implode('\', \'', $catids);
			$where[] = "`catid` in ('{$cids}')";
		}
		if(false != $is_parent) {
			$where[] = "`parentid` = 0";
		}
		$where = 'where ' . implode(' and ', $where);
		$data = array();
		foreach($this->mysql()->find_all("select * from `v9_category` {$where} order by listorder asc;") as $d) {
			$data[$d['catid']] = $d;
		}
		return $data;
	}
	
	/*
	 * 来源信息
	 */
	function get_copyfrom($siteid){
	
		$siteid = intval($siteid);
		if(empty($siteid)){
			return false;
		}
	
		$sql = "select * from `v9_copyfrom` where siteid='{$siteid}' order by id asc";
		return $this->mysql()->find_all($sql);
	}
	
	function get_copyfrom_byid($id){
	
		$id = intval($id);
		if(empty($id)){
			return false;
		}
	
		$sql = "select * from `v9_copyfrom` where id='{$id}' limit 1;";
		return $this->mysql()->find_one($sql);
	}
	
	
	function make_static_path($curr_cat) {
	
		$setting = array();
		eval("\$setting = {$curr_cat['setting']};");
		$show_ruleid = isset($setting['category_ruleid']) ? $setting['category_ruleid'] : '1';

		$urlrule = ujn_model::get_instance('ujn_model')->mysql()->find_one("select * from `v9_urlrule` where `urlruleid`='{$show_ruleid}' limit 1;");
		$urlrules_arr = explode('|',$urlrule['urlrule']);

		$urlrule = $curr_cat['page'] <= 1 ? $urlrules_arr[0] : $urlrules_arr[1];
	
		$urls = str_replace(
				array('{$categorydir}','{$catdir}','{$catid}','{$page}'),
				array($curr_cat['parentdir'], $curr_cat['catdir'], $curr_cat['catid'], $curr_cat['page']),
				$urlrule
		);
		return '/'.$urls;
	}
	
  /**
  * @return mdl_category
  */
  static function get_instance($empty = '') {
    return parent::get_instance(__CLASS__);
  }
}